package com.bojin.studio.ubattery;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ActivityManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Debug;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ProcessFragment extends Fragment{
ListView lv=null;
ArrayList<Map<String,Object>> al=null;
ActivityManager mActivityManager;
public ProcessFragment() {
    	
    }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivityManager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager.getRunningAppProcesses();  
	PackageManager pm = getActivity().getPackageManager();
	al=new ArrayList<Map<String,Object>>();
	for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) 
	{
		Map<String,Object> item=new HashMap<String,Object>();
		int pid = appProcessInfo.pid;
		int[] myMempid = new int[] { pid };
		Debug.MemoryInfo[] memoryInfo = mActivityManager.getProcessMemoryInfo(myMempid);
		int memSize = memoryInfo[0].dalvikPrivateDirty;
		try
		{
		CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(appProcessInfo.processName, PackageManager.GET_META_DATA));
		DecimalFormat   df   =   new   DecimalFormat(   "#0.0 "); 
		
		item.put("image",pm.getApplicationIcon(appProcessInfo.processName) +"");
		item.put("app", c.toString());
		item.put("mem", String.valueOf(df.format(memSize/1024.0))+"MB");
		//fio.write("record.txt",c.toString()+" "+String.valueOf(df.format(memSize/1024.0))+"MB");   
		}
		catch(Exception e) 
		{
			
		}
		al.add(item);	
	}
  
  /* for (int i=0;i<10;i++)
   {
	   Map<String,Object> item=new HashMap<String,Object>();
	   item.put("image", android.R.drawable.ic_menu_month);
	   item.put("app", "Facebook");
	   item.put("mem", "30MB");
	   al.add(item);
   }*/
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
	View view=inflater.inflate(R.layout.fragment_process, container, false); 
	setupViewComponent(view);
	return view;
}

public void setupViewComponent(View view)
{
	lv=(ListView)view.findViewById(R.id.listView1);
	SimpleAdapter adapter=new SimpleAdapter(getActivity(),
			                                al,
			                                R.layout.item_process,
			                                new String[]{"image","app","mem"},
	    	                                new int[] {R.id.imageView1,R.id.textView1,R.id.textView2});
	lv.setAdapter(adapter);
}

/*public class AppsAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<ResolveInfo> mApps;

    public AppsAdapter(Context context, List<ResolveInfo> mApps) {
        this.inflater = LayoutInflater.from(context);
        this.mApps = mApps;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHendler hendler;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.row_view, null);
            hendler = new ViewHendler();
            hendler.textLable = (TextView)convertView.findViewById(R.id.textViewLable);
            hendler.iconImage = (ImageView)convertView.findViewById(R.id.imageViewIcon);
            convertView.setTag(hendler);
        } else {
            hendler = (ViewHendler)convertView.getTag();
        }
        ResolveInfo info = this.mApps.get(position);
        hendler.iconImage.setImageDrawable(info.loadIcon(getPackageManager()));
        hendler.textLable.setText(info.loadLabel(getPackageManager()));

        return convertView;

    }
    class ViewHendler{
    	TextView memInfo;
        TextView textLable;
        ImageView iconImage;
    }


    public final int getCount() {
        return mApps.size();
    }

    public final Object getItem(int position) {
        return mApps.get(position);
    }

    public final long getItemId(int position) {
        return position;
    }
}*/

}
